<?php

namespace admin\helper;

use Qiniu\Auth;
use Qiniu\Storage\UploadManager;

class qiniu
{

    //public static $uploadUrl = 'http://up-na0.qiniu.com';
    public static $uploadUrl = 'http://up-z2.qiniu.com';
    public static $accessKey = '6YDw3z5x27nJ_ryLr2o94J-7arAgG8E121dTr5fa';
	// public static $accessKey = '7zyjNTFEic5m34x5oLCrl4v0iXIqm_YyGzwp_DGG';
	//public static $secretKey = 'LmpUA2cr50kVCUbNL6EOywdWFnqyAUUN4xRT-whq';
    public static $secretKey = 'c6vS98GcJmyWWlkW31LAY2IMsX9dI5PP9cqkQmiL';
    //public static $bucket    = 'bucketcn'; //华南的bucket名称
	public static $bucket    = 'bucket'; //华南的bucket名称
	
    //public static $ext       = "-shop"; // 图片样式
	public static $ext       = "-shop"; // 图片样式
	//public static $ext       = ""; // 图片样式
    public static $imgUrlCn  = 'http://ddncn.bgnht.com/'; //国内
    public static $videoUrl =  'http://video.bgnht.com/';//vedio
    public static $imgUrl    = 'http://ddncn.bgnht.com/'; //国外

    public static function getToken($bucket = "")
    {
        if (empty($bucket)) {
            $bucket = self::$bucket;
        }
        $auth = new Auth(self::$accessKey, self::$secretKey);
        return $auth->uploadToken($bucket);
    }

    public static function getKey($type, $ext)
    {
        if (empty($type)) {
            $type = "um";
        }
        if ($ext) {
            return $type . '/' . date('Y') . date('m') . date("d") . '/' . time() . rand(100, 999) . '.jpg';
        }
        else {
            return $type . '/' . date('Y') . date('m') . date("d") . '/' . time() . rand(100, 999) . '.mp4';
        }
    }

    public static function changImgDomain($imgUrl)
    {
        return str_replace(self::$imgUrlCn, self::$imgUrl, $imgUrl);
    }

    public static function upload($type)
    {
        $info     = ['state' => 'FAIL'];
        $file     = $_FILES['upfile'];
        // 要上传文件的本地路径
        $filePath = $file['tmp_name'];
        $ext      = self::$ext;

        $filetype = $file['type'];
        $url = self::$imgUrlCn;
        if(strstr($filetype, 'video')){
            $url = self::$videoUrl;
        }

        if (!strstr($filetype, 'image')) {
            $ext = '';
        }
        else {
            $filetype = 'application/octet-stream';
        }
        // 上传到七牛后保存的文件名
        $key       = self::getKey($type, $ext);
        $token     = self::getToken();
        // 初始化 UploadManager 对象并进行文件的上传。
        $uploadMgr = new UploadManager();
        // 调用 UploadManager 的 putFile 方法进行文件的上传。
        list($ret, $err) = $uploadMgr->putFile($token, $key, $filePath, null, $filetype);
        if ($err == null) {
            $filename = $ret['key'];
            $info     = array(
                "state"    => 'SUCCESS',
                "url"      => $url . $filename . $ext,
				//"url"      => $url . $filename . $ext,
                "title"    => $key,
                "original" => $file['name'],
                "type"     => 'jpg',
                "size"     => $file['size'],
				"filename"     => $filename,
				"ext"     => $ext,
				"url"      => $url . $filename,
				
            );
        }
        return $info;
    }

    public static  function uploadImgTxt($type,$file){
        $info     = ['state' => 'FAIL'];
        // 要上传文件的本地路径
        $ext      = self::$ext;
        //$filetype = $file['type'];
        $url = self::$imgUrlCn;
        $filetype = 'application/octet-stream';
        // 上传到七牛后保存的文件名
        $key       = self::getKey($type, $ext);
        $token     = self::getToken();
        // 初始化 UploadManager 对象并进行文件的上传。
        $uploadMgr = new UploadManager();
        // 调用 UploadManager 的 putFile 方法进行文件的上传。
        list($ret, $err) = $uploadMgr->putFile($token, $key, $file, null, $filetype);
        if ($err == null) {
            $filename = $ret['key'];
            $info     = array(
                "state"    => 'SUCCESS',
                "url"      => $url . $filename . $ext,
                "title"    => $key,
                //"original" => $file['name'],
                "type"     => 'jpg',
                //"size"     => $file['size']
            );
        }
        return $info;
    }
}
